SQL Server - 分組、擁有和計數 (SQL Server - Group by, having and count in a mix)


問題描述

SQL Server ‑ 分組、擁有和計數 (SQL Server ‑ Group by, having and count in a mix)

I have a database with a long list of records. Most of the columns have foreign keys to other tables.

Example:

ID SectorId BranchId
‑‑ ‑‑‑‑‑‑‑‑ ‑‑‑‑‑‑‑‑
5  3        5

And then I will have a table with sectors, branches ect.

My issue:

I want to know how many records which has sector 1, 2, 3 ... n. So what I want is a group by Sector and then some count(*) which will tell me how many there is of each.

Expected output

So for instance, if I have 20 records the result might look like this:

SectorId Count
‑‑‑‑‑‑‑‑ ‑‑‑‑‑
1        3
2        10
3        4
4        6

My attempts so far

I do not normally work a lot with databases and I have been trying to solve this for 1.5 hours. I have tried something like this:

SELECT COUNT(*)
FROM Records r
GROUP BY r.Sector
WHERE r.Date BETWEEN '2011‑01‑01' AND '2011‑12‑31'

But... errors and problems all over!

I would really appreciate some help. I do know this is probably very simple.

Thanks!

‑‑‑‑‑

參考解法

方法 1:

The sequence of your query is not correct; it should be like this: ‑

SELECT COUNT(*)
FROM Records r
WHERE r.Date BETWEEN '2011‑01‑01' AND '2011‑12‑31'
GROUP BY r.Sector

The output will be only counts i.e.

count
‑‑‑‑‑
3
10
4
6

If you want to fetch both sector and count then you need to modify the query a little

SELECT r.Sector, COUNT(*) as Count
FROM Records r
WHERE r.Date BETWEEN '2011‑01‑01' AND '2011‑12‑31'
GROUP BY r.Sector

The output will be like this: ‑

Sector Count
‑‑‑‑‑‑ ‑‑‑‑‑
1      3
2      10
3      4
3      6

方法 2:

Your query was partially right,But it needs some modification.

If I write this way:‑

SELECT r.SectorID,COUNT(*) AS count
FROM Records r
WHERE r.Date BETWEEN '2011‑01‑01' AND '2011‑12‑31'
GROUP BY r.SectorID

Then output will be:‑

SectorID  Count 
1           3
2          10
3           4
4           6

(by Lars HoldgaardWaqas RajaBhaskar saha)

參考文件

  1. SQL Server ‑ Group by, having and count in a mix (CC BY‑SA 3.0/4.0)

#ASP.NET #SQL #sql-server #Database






相關問題

System.Reflection.Assembly.LoadFile 鎖定文件 (System.Reflection.Assembly.LoadFile Locks File)

如何在沒有全局變量的情況下一直保留我的變量? (How can I keep my variable all the time without global variables?)

C# / ASP.NET - Web 應用程序鎖定 (C# / ASP.NET - Web Application locking)

關閉模態對話框窗口後 ASP.NET 刷新父頁面 (ASP.NET Refresh Parent Page after Closing Modal Dialog Window)

無法將 NULL 值傳遞給數據庫 (Unable to pass NULL value to database)

wcf:將用戶名添加到消息頭是否安全? (wcf: adding username to the message header is this secure?)

使用 ASP.Net 教初學者 Web 開發的小項目想法 (Small projects ideas to teach beginners web development using ASP.Net)

SQL Server - 分組、擁有和計數 (SQL Server - Group by, having and count in a mix)

企業庫異常處理應用程序塊和日誌記錄應用程序塊在 ASP.NET 中的正確使用 (Enterprise Library Exception Handling Application Block and Logging Application Block proper use in ASP.NET)

來自proc的asp.net多個結果集:是否有必要將結果映射到類?如果是這樣,怎麼做? (asp.net multiple result set from proc: is it necessary to map results to class? If so, how?)

如何在測試工具中實例化 asp.net 代碼隱藏類? (How can I instantiate an asp.net codebehind class in a test harness?)

Web 窗體用戶控制事件,需要在頁面加載後添加 (Web Form User Control Event, needs to be added after page loads)







留言討論